package ddd6;

import java.io.File;

public class work2 {
    public static void main(String[] args) {
        String directoryPath = "E:/delete1";
        File directory = new File(directoryPath);

        if (deleteDirectory(directory)) {
            System.out.println("文件夹删除成功！");
        } else {
            System.out.println("文件夹删除失败！");
        }
    }
    public static boolean deleteDirectory(File dir) {
        if (!dir.exists()) {
            System.out.println("文件夹不存在: " + dir.getAbsolutePath());
            return false;
        }
        File[] files = dir.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    deleteDirectory(file);
                } else {
                    if (file.delete()) {
                        System.out.println("已删除文件: " + file.getAbsolutePath());
                    } else {
                        System.err.println("删除文件失败: " + file.getAbsolutePath());
                        return false;
                    }
                }
            }
        }
        if (dir.delete()) {
            System.out.println("已删除目录: " + dir.getAbsolutePath());
            return true;
        } else {
            System.err.println("删除目录失败: " + dir.getAbsolutePath());
            return false;
        }
    }
}
